<?php
/**
 * 相册
 * Enter description here ...
 * @author nijn
 *
 */
class PhotoBehavior extends CActiveRecordBehavior {

	/**
	 * 分类id
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $cateIds = array();
	
	/**
	 * 是否有相册
	 * Enter description here ...
	 */
	public function isHasPhoto() {
		$owner = $this->owner;
		if (in_array(CHtml::value($owner, 'info.category_id'), $this->cateIds)) {
			return true;
		}
		return false;
	}
	
	/**
	 * 取得公司名称
	 * Enter description here ...
	 */
	public function companyModelName() {
		$owner = $this->owner;
		switch (CHtml::value($owner, 'info.category_id')) {
			case 2:	return 'CompanyHotel';
			case 3:	return 'CompanyScenic';
			default: return '';
		}
	}

	public function getPhotos() {
		$modelName = $this->companyModelName();
		$model = new $modelName();
		
		return $model->findAll($this->criteriaPhoto());
	}
	
	public function photoSearch($pageSize = 10) {
		$modelName = $this->companyModelName();
		$model = new $modelName();
		
		return new CActiveDataProvider($model, array(
			'criteria'=>$this->criteriaPhoto(),
			'pagination' => array(
				'pageSize' => $pageSize,
			)
		));
	}
	
	protected function criteriaPhoto() {
		$owner = $this->owner;
		$attribute = CHtml::value($owner, 'info.category_id') == 2 ? 'profile.hotel_id' : 'profile.scenic_id';
		$goodsModelName = CHtml::value($owner, 'info.category_id') == 2 ? 'Hotel' : 'Scenic';
		$goodsCriteria = new CDbCriteria();
		$goodsCriteria->with = array(
			'profile'
		);
		$goodsCriteria->compare('provider_id', $owner->id);
		$goodsCriteria->compare('status', 'ST_PASSED');
		$goodsCriteria->compare('listing_status', 'ST_LISTING');
		$goodsModel = new $goodsModelName;
		$goods = $goodsModel->findAll($goodsCriteria);
		
		$criteria = new CDbCriteria();
		$criteria->select = 't.*';
		$criteria->with = array(
			'photo',
		);
		$criteria->addCondition('photo.id IS NOT NULL');
		$criteria->addInCondition('object_id', CHtml::listData($goods, $attribute, $attribute));
		$criteria->group = 't.id';
		return $criteria;
	}
}

?>